<template>
	<view class="pages overflow-auto pad-b-80">
		<view class="mar-10 border-rad-3 border-type-1">
			<view
				v-for="(item, index) in tableData"
				:key="index"
				class="flex align-center"
				:style="{
					borderBottom: index !== tableData.length -1 ? '1px #e6e6e6 solid' : 'none',
				}"
			>
				<view class="flex align-center w-px-120 pad-x-10" style="min-height: 26px;">
					<text>{{ item.DESCR }}</text>
				</view>
				
				<view
					class="flex flex-1 align-center"
					:style="{
						borderLeft: '1px #e6e6e6 solid',
						minHeight: '26px',
						padding: item.TYPE !== 'textarea' ? '10px 10px' : '10px 0px',
					}"
				>
					<text v-if="item.TYPE === 'disabled'" class="color-333333">{{ item.DESCR_CVAL }}</text>
					<view v-else class="w-100">
						<view v-if="item.TYPE === 'input'">
							<view
								v-if="item.DESCR === '车号'"
								class="flex align-center"
							>
								<input
									type="text"
									v-model="item.DESCR_CVAL"
									:placeholder="item.placeholder"
									:disabled="item.isEdit"
									style="color: #333333; height: 30px;"
									@blur="handleBlur($event, item)"
								/>
								
								<view class="w-px-80 mar-l-10">
									<u-button type="primary" color="#bd3124" @click="handleSearch()">查询</u-button>
								</view>
							</view>
							
							<input
								v-else
								type="text"
								v-model="item.DESCR_CVAL"
								:placeholder="item.placeholder"
								:disabled="item.isEdit"
								style="color: #333333; height: 30px;"
							/>
						</view>
						
						<view v-else-if="item.TYPE === 'textarea'" class="w-100">
							<textarea
								v-model="item.DESCR_CVAL"
								:placeholder="item.placeholder"
								:disabled="item.isEdit"
								auto-height
								style="width: auto; color: #333333; margin: 10px; height: 30px;"
							></textarea>
						</view>
					</view>
				</view>
			</view>
		</view>
		
		<view class="mar-10 border-rad-3 border-type-1">
			<view
				v-for="(item, index) in tableData2"
				:key="index"
				class="flex align-center"
				:style="{
					borderBottom: index !== tableData2.length -1 ? '1px #e6e6e6 solid' : 'none',
				}"
			>
				<view class="flex align-center w-px-120 pad-x-10" style="min-height: 26px;">
					<text>{{ item.DESCR }}</text>
				</view>
				
				<view
					class="flex flex-1 align-center"
					:style="{
						borderLeft: '1px #e6e6e6 solid',
						minHeight: '26px',
						padding: item.TYPE !== 'textarea' ? '10px 10px' : '10px 0px',
					}"
				>
					<text v-if="item.TYPE === 'disabled'" class="color-333333">{{ item.DESCR_CVAL }}</text>
					<view v-else class="w-100">
						<view v-if="item.TYPE === 'input'">
							<view
								v-if="item.DESCR === '车号'"
								class="flex align-center"
							>
								<input
									type="text"
									v-model="item.DESCR_CVAL"
									:placeholder="item.placeholder"
									:disabled="item.isEdit"
									style="color: #333333; height: 30px;"
									@blur="handleBlur($event, item)"
								/>
								
								<view class="w-px-80 mar-l-10">
									<u-button type="primary" color="#bd3124" @click="handleSearch()">查询</u-button>
								</view>
							</view>
							
							<input
								v-else
								type="text"
								v-model="item.DESCR_CVAL"
								:placeholder="item.placeholder"
								:disabled="item.isEdit"
								style="color: #333333; height: 30px;"
							/>
						</view>
						
						<view v-else-if="item.TYPE === 'textarea'" class="w-100">
							<textarea
								v-model="item.DESCR_CVAL"
								:placeholder="item.placeholder"
								:disabled="item.isEdit"
								auto-height
								style="width: auto; color: #333333; margin: 10px; height: 30px;"
							></textarea>
						</view>
					</view>
				</view>
			</view>
		</view>
		
		<view
			class="pos-f bg-white flex align-center shadow-type-2 pad-x-10"
			style="left: 0; right: 0; bottom: 0; height: 60px;"
		>
			<u-button type="primary" color="#bd3124" @click="handleBind()">确定绑定</u-button>
		</view>
	</view>
</template>

<script>
	import Utils from '@/utils/utils'
	import { mapState } from 'vuex'
	import { getApplyForRecordsDetail, repairBind } from '@/apis/replaceApplyForRecords/index'
	
	export default {
		computed: {
			...mapState({
				token: state => state.user.token,
				longitude: state => state.user.longitude,
				latitude: state => state.user.latitude,
			}),
		},
		
		data() {
			return {
				orderId: null, // 上一页的订单 id
				tableData: [],
				tableData2: [],
			}
		},
		
		onLoad(options) {
			this.orderId = options.id
			this.init()
			this.findDetails()
		},
		
		methods: {
			// 初始化
			init() {
				this.tableData = [
					{
						DESCR: '申请单号',
						DESCR_CVAL: '',
						TYPE: 'disabled',
						isEdit: false,
					}, {
						DESCR: '车号',
						DESCR_CVAL: '',
						TYPE: 'disabled',
						isEdit: false,
					}, {
						DESCR: '申请时间',
						DESCR_CVAL: '',
						TYPE: 'disabled',
						isEdit: false,
					}, {
						DESCR: '申请人',
						DESCR_CVAL: '',
						TYPE: 'disabled',
						isEdit: false,
					}, {
						DESCR: '申请人手机号',
						DESCR_CVAL: '',
						TYPE: 'disabled',
						isEdit: false,
					}, {
						DESCR: '申请人所属机构',
						DESCR_CVAL: '',
						TYPE: 'textarea',
						isEdit: true,
					}, {
						DESCR: '反馈人',
						DESCR_CVAL: '',
						TYPE: 'disabled',
						isEdit: false,
					}, {
						DESCR: '更换原因',
						DESCR_CVAL: '',
						TYPE: 'textarea',
						isEdit: true,
					}
				]
				
				this.tableData2 = [
					{
						DESCR: '机械环保代码',
						DESCR_CVAL: '',
						TYPE: 'textarea',
						isEdit: true,
					}, {
						DESCR: '变更前终端型号',
						DESCR_CVAL: '',
						TYPE: 'textarea',
						isEdit: true,
					}, {
						DESCR: '变更后终端型号',
						DESCR_CVAL: '',
						TYPE: 'textarea',
						isEdit: true,
					}, {
						DESCR: '变更前芯片型号',
						DESCR_CVAL: '',
						TYPE: 'textarea',
						isEdit: true,
					}, {
						DESCR: '变更后芯片型号',
						DESCR_CVAL: '',
						TYPE: 'textarea',
						isEdit: true,
					}, {
						DESCR: '变更前芯片ID',
						DESCR_CVAL: '',
						TYPE: 'textarea',
						isEdit: true,
					}, {
						DESCR: '变更后芯片ID',
						DESCR_CVAL: '',
						TYPE: 'disabled',
						isEdit: false,
					}, {
						DESCR: '变更前终端编码',
						DESCR_CVAL: '',
						TYPE: 'textarea',
						isEdit: true,
					}, {
						DESCR: '变更后终端编码',
						DESCR_CVAL: '',
						TYPE: 'textarea',
						isEdit: true,
					}
				]
			},
			
			// 查询更换申请详情
			findDetails() {
				let params = {
					terminalRno: this.orderId,
					token: this.token
				}
				
				Utils.loading('查询中...')
				
				getApplyForRecordsDetail(params).then(res => {
					uni.hideLoading()
					this.showData(res.Result)
				}).catch(err => {
					uni.hideLoading()
				})
			},
			
			// 根据查到的详情，回显数据
			showData(row) {
				this.tableData[0].DESCR_CVAL = this.orderId // 申请单号
				this.tableData[1].DESCR_CVAL = row.vehicleNumber // 车号
				this.tableData[2].DESCR_CVAL = row.renewalDate // 申请时间
				this.tableData[3].DESCR_CVAL = row.applicant // 申请人
				this.tableData[4].DESCR_CVAL = row.applicantTel // 申请人手机号
				this.tableData[5].DESCR_CVAL = row.applicantOrg // 申请人所属机构
				this.tableData[6].DESCR_CVAL = row.feedbackPerson // 反馈人
				this.tableData[7].DESCR_CVAL = row.feedbackDescription // 更换原因
				this.tableData2[0].DESCR_CVAL = row.epCode // 机械环保代码
				this.tableData2[1].DESCR_CVAL = row.terminalUnitType // 变更前终端型号
				this.tableData2[2].DESCR_CVAL = row.terminalUnitTypeRenewal // 变更后终端型号
				this.tableData2[3].DESCR_CVAL = row.chipModel // 变更前芯片型号
				this.tableData2[4].DESCR_CVAL = row.chipModelRenewal // 变更后芯片型号
				this.tableData2[5].DESCR_CVAL = row.chipModelId // 变更前芯片ID
				this.tableData2[6].DESCR_CVAL = row.chipModelIdRenewal // 变更后芯片ID
				this.tableData2[7].DESCR_CVAL = row.terminalUnitNcode // 变更前终端编码
				this.tableData2[8].DESCR_CVAL = row.terminalUnitNcodeRenewal // 变更后终端编码
				this.tableData3[0].REASON = row.auditStep // 状态
			},
			
			// 维修绑定
			handleBind() {
				if (!this.tableData[1].DESCR_CVAL) {
					Utils.popUpMsg('none', '车号不存在，请检查！', 3000)
					return
				}
				
				Utils.modal('提示', '确定绑定吗？', res => {
					if (res.confirm) {
						let params = {
							token: this.token,
							vehicleNumber: this.tableData[1].DESCR_CVAL
						}
						
						Utils.loading('正在绑定...')
						
						repairBind(params).then(res => {
							uni.hideLoading()
							Utils.popUpMsg('success', res.msg, 1000)
							setTimeout(() => {
								uni.navigateBack({ delta: 1 })
							}, 1000)
						}).catch(err => {
							uni.hideLoading()
						})
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.pages {
		position: absolute;
		left: 0;
		right: 0;
		top: 0;
		bottom: 0;
		background-color: #fff;
	}
</style>
